//
//  AIKefuView.m
//  HNBaseProject
//
//  Created by xujie on 2024/7/19.
//

#import "AIKefuView.h"

@implementation AIKefuView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    
    self.width = kScreenW - 60;
    self.height = 291;
    self.layer.cornerRadius = 15;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor colorWithHexString:@"#DBEDFF"];

    
    UILabel *topLab = [[UILabel alloc] init];
    [self addSubview:topLab];
    topLab.text =  NSLocalizedString(@"拨打客服电话", nil);
    topLab.font = PFRFontMedium(15);
    topLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [topLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.offset(24);
        make.height.mas_equalTo(21);
    }];
    
    UIImageView *leftV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kefu_left"]];
    [self addSubview:leftV];
    [leftV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topLab);
        make.right.mas_equalTo(topLab.mas_left).offset(-9);
    }];
    
    UIImageView *rightV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"kefu_right"]];
    [self addSubview:rightV];
    [rightV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topLab);
        make.left.mas_equalTo(topLab.mas_right).offset(9);
    }];
    
    
    UILabel *tipLab = [[UILabel alloc] init];
    [self addSubview:tipLab];
    tipLab.text =  NSLocalizedString(@"专属热线", nil);;
    tipLab.font = PFRFontMedium(16);
    tipLab.textColor = [UIColor colorWithHexString:@"#666666"];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(topLab.mas_bottom).offset(44);
    }];
    
    MJWeakSelf
    UILabel *cancleLab = [[UILabel alloc] init];
    cancleLab.textAlignment = NSTextAlignmentCenter;
    cancleLab.text =  NSLocalizedString(@"取消", nil);//@"取消";
    cancleLab.textColor = [UIColor colorWithHexString:@"#7D7C81"];
    cancleLab.font = PFRFont(14);
    cancleLab.layer.borderWidth = 1;
    cancleLab.layer.borderColor = [UIColor colorWithHexString:@"#CCCCCC"].CGColor;
    cancleLab.userInteractionEnabled = YES;
    cancleLab.layer.cornerRadius = 21;
    cancleLab.layer.masksToBounds = YES;
    [cancleLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.cancleClick) {
            weakSelf.cancleClick();
        }
    }]];
    [self addSubview:cancleLab];
    [cancleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(42);
        make.width.mas_equalTo(128);
        make.bottom.offset(-25);
        make.left.offset(24);
    }];
    
    UILabel *sureLab = [[UILabel alloc] init];
    sureLab.textAlignment = NSTextAlignmentCenter;
    sureLab.text = NSLocalizedString(@"立即拨打", nil);//@"立即拨打";
    sureLab.textColor = [UIColor whiteColor];
    sureLab.font = PFRFont(14);
    sureLab.userInteractionEnabled = YES;
    sureLab.layer.cornerRadius = 21;
    sureLab.layer.masksToBounds = YES;
    sureLab.backgroundColor = [UIColor colorWithHexString:@"#1D8EFF"];
    [sureLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf makePhone];
    }]];
    [self addSubview:sureLab];
    [sureLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(42);
        make.width.mas_equalTo(128);
        make.bottom.offset(-25);
        make.right.offset(-24);
    }];
    
    UILabel *timeLab = [[UILabel alloc] init];
    timeLab.text = [NSString stringWithFormat:@"%@09:30 — 17:00",NSLocalizedString(@"工作时间:", nil)];
    timeLab.font = PFRFont(16);
    timeLab.textColor = [UIColor colorWithHexString:@"#666666"];
    [self addSubview:timeLab];
    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.offset(-106);
    }];
    
    self.phoneLab = [[UILabel alloc] init];
    [self addSubview:self.phoneLab];
    self.phoneLab.textColor = [UIColor colorWithHexString:@"#1D8EFF"];
    self.phoneLab.font = PFRFontSemibold(21);
    self.phoneLab.text = @"";
    [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(timeLab.mas_top).offset(-12);
    }];
    [self getPhoneNum];
}

- (void)makePhone {
    if (self.sureClick) {
        self.sureClick(self.phoneLab.text);
    }
}

- (void)getPhoneNum {

    MJWeakSelf
    [[DCHttpClient shareClient] requestWithPath:@"api/user/contactUs" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            weakSelf.phoneLab.text = @"";
        } else {
            [weakSelf xjmakeToast:dict[@"msg"]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
       
    }];
}

@end
